﻿using Core.DataAccess.Model.Project.Information;
using Core.Framework.Model.Common;
using Core.Framework.Util;
using Core.IBusiness.IInformation;
using Core.IBusiness.IInformation.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace Core.Web.Controllers.Information
{
    /// <summary>
    /// 信息分类管理
    /// </summary>
    [Route(".v1/Information/[controller]")]
    [ApiController]
    [ApiVerification(IBusiness.IProjectModule.Model.APIServiceEnum.BBS)]
    public class InformationTypeController : BaseApiController
    {
        public InformationTypeController(IInformation iInformation) :
            base(iInformation: iInformation)
        {

        }
        
        #region Serach

        /// <summary>
        /// 查询指定分类信息含子集属性
        /// </summary>
        /// <param name="key">分类关键字</param>
        /// <returns></returns>
        [HttpGet("{key}")]
        [IgnoreAuthorize]
        public IResult PostGetTypeByKey(int key)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.GetInformationType(
                        key,
                        iProjectInfo.Token));
                },
                new Tuple<bool, string>(key > 0, key > 0 ? "" : "params: key is null"));
        }


        /// <summary>
        /// 查询分类信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("getTypes")]
        [IgnoreAuthorize]
        public IResult PostGetType(Pagination pagination)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.GetInformationTypes(
                        iProjectInfo.Token,
                        pagination), pagination);
                },
                pagination.IsValid(IsValidEnum.select));
        }



        /// <summary>
        /// 查询指定用户分类信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("getUserTypes")]
        [IgnoreAuthorize]
        public IResult PostGetUserType(Pagination pagination)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.GetInformationTypeUser(
                        iClientInfo.Key,
                        iProjectInfo.Token,
                        pagination), pagination);
                },
                pagination.IsValid(IsValidEnum.select));
        }

        #endregion







        #region InfoType exec

        /// <summary>
        /// 添加分类信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IResult Post(InformationType model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationTypeExec(
                        model,
                        SqlExecType.insert));
                }, model.IsValid(IsValidEnum.reg));
        }

        /// <summary>
        /// 修改分类信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public IResult Put(InformationType model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationTypeExec(
                        model,
                        SqlExecType.update));
                }, model.IsValid(IsValidEnum.update));
        }

        /// <summary>
        /// 删除分类信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IResult Delete(int id)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationTypeExec(
                        new InformationType
                        {
                            Id = id,
                            ProjectToken = iProjectInfo.Token
                        },
                        SqlExecType.delete));
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }






        /// <summary>
        /// 添加分类属性信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost(".attr")]
        public IResult PostAttr(InformationTypeAttribute model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationTypeAttributeExec(
                        model,
                        SqlExecType.insert));
                }, model.IsValid(IsValidEnum.reg));
        }

        /// <summary>
        /// 修改分类属性信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut(".attr")]
        public IResult PutAttr(InformationTypeAttribute model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationTypeAttributeExec(
                        model,
                        SqlExecType.update));
                }, model.IsValid(IsValidEnum.update));
        }

        /// <summary>
        /// 删除分类属性信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete(".attr{id}")]
        public IResult DeleteAttr(int id)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationTypeAttributeExec(
                        new InformationTypeAttribute
                        {
                            Id = id
                        },
                        SqlExecType.delete));
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }
        #endregion


    }
}
